/**
 * 
 */
$("#btnShow").click(function(){
	if (window.openDatabase) {
//		persistence.store.sqlite.config(persistence, "jquerymobile", 'database', 5 * 1024 * 1024);
		persistence.store.websql.config(persistence, "jquerymobile", 'database', 5 * 1024 * 1024);
	} else {
		persistence.store.memory.config(persistence);
	}
	
	defineObjects();
	
	var Orario = defineOrario();
	
	var orarioCompleto = Orario.all().prefetch('giorno').prefetch('ora').prefetch('materia');
	
	orarioCompleto.list(null, function(results){
		var execute = true;
		results.forEach(function(orario){
			if (execute){
				alert ("Giorno: " + orario.giorno.descrizione + ", Ora: " +orario.ora.descrizione);
				execute = false;
			}
		});
	});
});




$( document ).ready(function() {
	  // Handler for .ready() called.


	 creaGridOrario();
	
	 $('#busy').hide();



	  
	 $(document).on('change', 'select', function() {
		    //do stuff
		 str =  $(this).find(":selected").Text;
		 var color ='#'+$(this).val();

		 $(this).prev().css('background-color',color);
		});
	 
    
});	  
	
var arrMateria= new Array();
var arrMateriaColor= new Array();;
function creaGridOrario()
{

	
	db = window.openDatabase("DiarioDB", "1.0", "PhoneGap Diaro", 200000);
	// lista materie
	
	db.transaction(function(tx) 
			{
				tx.executeSql("SELECT * FROM materia ORDER BY descrizione", [], function (tx, resultMaterie) {
				result = resultMaterie;
				for ( var i = 0; i < result.rows.length; i++) 
				{
					arrMateria[i]=result.rows.item(i).descrizione;
					arrMateriaColor[i]=result.rows.item(i).color;
				
				}
				});
			});
	
	var result;
	var resultSettimana;
	
	db.transaction(function(tx) {
		
			tx.executeSql("SELECT * FROM giorno ORDER BY id_giorno, descrizione", [], function (tx, resultSettimana) {
				resultSettimana = resultSettimana;
				$('#gridOrario').append('<fieldset style="width:100%;" id="container_0"  data-role="controlgroup" data-type="horizontal"></fieldset>');
				
			
				for ( var i = 0; i < resultSettimana.rows.length; i++) 
				  {
					giornoSettimana=resultSettimana.rows.item(i).descrizione;
					  $('#container_0').append('<div class="ui-block-b" style="width:14%;text-align:center"><div class="ui-bar ui-bar-b">'+giornoSettimana+ '</div></div>');
				  }	
				
				
			});	
			tx.executeSql("SELECT * FROM orario ORDER BY id_ora, id_giorno", [], function (tx, resultOrario) {
				result = resultOrario;
				
			
			var oraPrec=0;
			var ora=0;
			for ( var i = 0; i < result.rows.length; i++) 
			  {
				ora=result.rows.item(i).id_ora;
				if (ora!= oraPrec)
					{
				$('#gridOrario').append('<fieldset style="width:100%;margin-top:-28px" data-role="controlgroup" data-type="horizontal" id="container_'+ora+'"></fieldset>').trigger("create");
			//		$('#gridOrario').append('<fieldset data-role="controlgroup" data-type="horizontal" class="ui-corner-all ui-controlgroup ui-controlgroup-horizontal" aria-disabled="false" data-disabled="false" data-shadow="false" data-corners="true" data-exclude-invisible="true" data-mini="false" data-init-selector=":jqmData(role="controlgroup")  id="container_'+ora+'"><div role="heading" class="ui-controlgroup-label"><legend>Horizontal controlgroup:</legend></div><div class="ui-controlgroup-controls"></div></fieldset>');
					}
			
				var fildset = '#container_'+ora;
				
				var selecTHtml =  creaSelect();
			
				  $(fildset).append( selecTHtml).trigger("create");
				
				  oraPrec = result.rows.item(i).id_ora;
			  }
		
		});
	});

	

}

var a=10;
function creaSelect()
{
	
	a++;
	var color;
	db = window.openDatabase("DiarioDB", "1.0", "PhoneGap Diaro", 200000);
	var selectHtml ='<select >';
	selectHtml +='<option value="#">..</option>';
	

				for ( var i = 0; i <arrMateria.length; i++) 
				{
				
				
					selectHtml +='<option value="'+arrMateriaColor[i]  +'">'+arrMateria[i]+'</option>';
				}
				selectHtml +='</select>';
				$('.ui-select .ui-btn-icon-right .ui-btn-inner, .ui-select .ui-li-has-count .ui-btn-inner').css('background-color',color);
				
				return selectHtml;
			 
		

}



